from HTMLTestRunner import HTMLTestRunner
import unittest
import time
import sys
sys.path.append("./test_case")
import threading
import queue
import re
from test_case import login_sta,shopping_sta
# 定义线程对象
emial_filename = 'None'
class MyThread(threading.Thread):
    def __init__(self, func, args=(), name="",testcase=""):
        super(MyThread, self).__init__(target=func, args=args, name=name)
        self.name = name
        self.func = func
        self.args = args
        self.myqueue = testcase# 设置queue，先进先出
    def run(self):
            while True:
                if self.myqueue.empty() == False:
                    suite = self.myqueue.get()
                    # print(str(suite))
                    self.func.run(suite)
                else:
                    break

# testcase = unittest.defaultTestLoader.discover('./test_case', pattern='party_sta.py')
# testcase2 = unittest.defaultTestLoader.discover('./test_case', pattern='application_process_sta.py')
# 'test_PartyMotifPartyPloy_change','test_PartyYearMotifPloy_change','test_PartySchoolPlantPlan_change','test_PartyPoliticsDisquisitionHarvest_change','test_PartyPlantInfo_change','test_PartyYearPlantPlan_change','test_PartyThreeTeamInfo_change','test_PartyAllowancePeopleInfo_change','test_PartytbExpertInfo_change','test_PartySecondLedgerInfo_change','test_PartyMothballManagerInfo_change','test_PartyManagerInfo_change'
def test(testcase=['test_PartyMotifPartyPloy_change'],n=1,report=1):
    myqueue = queue.Queue(maxsize=10)
    if report ==1:
        now = time.strftime("%Y-%m-%d %H_%M_%S")
        global emial_filename
        emial_filename = now + 'result.html'
        filename = './report/' + now + 'result.html'
        fp = open(filename, 'wb')
        runner = HTMLTestRunner(stream=fp, title='网页自动化测试', description='环境：windows 7 浏览器：firefox')
        # print('带测试报告')
    elif report ==0:
        runner = unittest.TextTestRunner()
        # print('不带测试报告')
    loginsuite = unittest.TestSuite()
    exprotsuite = unittest.TestSuite()
    authoritysuite = unittest.TestSuite()
    previewsuite = unittest.TestSuite()
    application_processsuite = unittest.TestSuite()
    Partysutie = unittest.TestSuite()
    for a in testcase:
        if re.search('login', a):
            loginsuite.addTest(login_sta.loginTest(a))
        elif re.search('exprot', a):
            exprotsuite.addTest(exprot_sta.ExprotTest(a))
        elif re.search('Preview', a):
            previewsuite.addTest(preview_sta.PreviewTest(a))
        elif re.search('Application', a):
            application_processsuite.addTest(application_process_sta.Application_Process_Test(a))
        elif re.search('authority', a):
            authoritysuite.addTest(authority_sta.authorityTest(a))
        elif re.search('shopping', a):
            Partysutie.addTest(shopping_sta.shoppingTest(a))
        else:
            print("没有找到符合条件的测试集")
    if loginsuite != unittest.TestSuite():
        myqueue.put(loginsuite)
    if exprotsuite != unittest.TestSuite():
        myqueue.put(exprotsuite)
    if authoritysuite != unittest.TestSuite():
        myqueue.put(authoritysuite)
    if previewsuite!=unittest.TestSuite():
        myqueue.put(previewsuite)
    if application_processsuite!= unittest.TestSuite():
        myqueue.put(application_processsuite)
    if Partysutie!= unittest.TestSuite():
        myqueue.put(Partysutie)
    for i in range(n):
        t = MyThread(runner,testcase=myqueue)
        t.start()
    for i in range(n):
        t.join()
    if report == 1:
        fp.close()

if __name__ == '__main__':
    test()


